package com.monitoringfloty.androidclient;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

public class GpsActivity extends Activity 
		implements LocationListener {
	
	
	private LocationManager lm;
	private EditText statusEdit;
	private EditText lonEdit;
	private EditText latEdit;
	private EditText speedEdit;
	private EditText altitudeEdit;
	private EditText accuracyEdit;
	static final String tag = "Main"; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gps);
		lm = (LocationManager) getSystemService(LOCATION_SERVICE);
		
		statusEdit = (EditText)findViewById(R.id.gpsEdit);
		lonEdit = (EditText)findViewById(R.id.lonEdit);
		latEdit = (EditText)findViewById(R.id.latEdit);
		speedEdit = (EditText)findViewById(R.id.speedEdit);
		altitudeEdit = (EditText)findViewById(R.id.altitudeEdit);
		accuracyEdit = (EditText)findViewById(R.id.accuracyEdit);
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, this);
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.gps, menu);
		return true;
	}

	@Override
	public void onLocationChanged(Location arg0) {
		// TODO Auto-generated method stub
		String latS = ""+arg0.getLatitude();
		String lonS = ""+arg0.getLongitude();
		String speedS = ""+arg0.getSpeed();
		String altS = ""+arg0.getAltitude();
		String accS= ""+arg0.getAccuracy()+" m";
		latEdit.setText(latS);
		lonEdit.setText(lonS);
		speedEdit.setText(speedS);
		altitudeEdit.setText(altS);
		accuracyEdit.setText(accS);
		statusEdit.setText("Fixed");
	}

	@Override
	public void onProviderDisabled(String arg0) {
		statusEdit.setText("GPS Disabled");
		
	}

	@Override
	public void onProviderEnabled(String arg0) {
		statusEdit.setText("GPS Enabled...Waiting");
	}

	@Override
	public void onStatusChanged(String arg0, int status, Bundle arg2) {
		switch (status) {
		case LocationProvider.OUT_OF_SERVICE:
			Log.v(tag, "Status Changed: Out of Service");
			statusEdit.setText("Out of service");
			break;
		case LocationProvider.TEMPORARILY_UNAVAILABLE:
			Log.v(tag, "Status Changed: Temporarily Unavailable");
			statusEdit.setText("Unavailable");
			break;
		case LocationProvider.AVAILABLE:
			Log.v(tag, "Status Changed: Available");
			statusEdit.setText("Available");
			break;
		}
	}
	
	@Override
	protected void onResume() {
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this);
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		lm.removeUpdates(this);
		super.onPause();
	}
	
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	Intent myIntent;
    	switch(item.getItemId()) {
	        case R.id.action_settings:
	            myIntent=new Intent(getBaseContext(),SettingsActivity.class);
	            startActivityForResult(myIntent,1);
	            //finish();
	            return true;
	            
	        default:
	            return super.onOptionsItemSelected(item);
    	}

    }

}
